package derby;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author chenzy
 * @since 2023/3/10
 *
 **/
public class DerbyDemo {
    private final Connection connection;

    public DerbyDemo() throws SQLException {
//        driver[className=org.apache.derby.iapi.jdbc.AutoloadedDriver@64bfbc86]
        var url="jdbc:derby:userDB;create=true;user=test;password=test";
        url="jdbc:derby://localhost:1527/chen;create=true";
        connection = DriverManager.getConnection(url);
        connection.setAutoCommit(false);
    }

    private void createTable() throws SQLException {
        var className="org.apache.derby.client.ClientAutoloadedDriver";

        Statement statement = connection.createStatement();
        /*By always和by default是说明生成这个IDENTITY的方式。By always是完全由系统自动生成。by default是可以由用户来指定一个值*/
        String sql = """
                create table "user"(
                    id bigint not null GENERATED ALWAYS AS IDENTITY,
                    name VARCHAR(20),
                    ps VARCHAR(20),
                    age int,
                    email VARCHAR(64),
                    constraint P_KEY_user primary key(id)
                )
                """;
        statement.execute(sql);
        sql = "create unique index USER_NAME_INDEX on \"user\" (name ASC)";
        statement.execute(sql);
        statement.close();
    }

    public static void main(String[] args) throws SQLException {
        var derbyDemo=new DerbyDemo();
        derbyDemo.createTable();

    }

}
